﻿using System;
using System.Collections.Generic;
using System.Linq;
using XcoAppSpaces.Core;
using XcoAppSpaces.Core.Ports;
using System.Threading;
using NUnit.Framework;

namespace test.XcoAppSpaces.Core
{
	[TestFixture]
	public class testLargePortSet
	{
		[Serializable] public class MsgType1 { }
		[Serializable] public class MsgType2 { }
		[Serializable] public class MsgType3 { }
		[Serializable] public class MsgType4 { }
		[Serializable] public class MsgType5 { }
		[Serializable] public class MsgType6 { }
		[Serializable] public class MsgType7 { }
		[Serializable] public class MsgType8 { }
		[Serializable] public class MsgType9 { }
		[Serializable] public class MsgType10 { }
		[Serializable] public class MsgType11 { }
		[Serializable] public class MsgType12 { }
		[Serializable] public class MsgType13 { }
		[Serializable] public class MsgType14 { }
		[Serializable] public class MsgType15 { }
		[Serializable] public class MsgType16 { }
		[Serializable] public class MsgType17 { }
		[Serializable] public class MsgType18 { }
		[Serializable] public class MsgType19 { }
		[Serializable] public class MsgType20 { }
		[Serializable] public class MsgType21 { }
		public class LargeWorkerContract : PortSet
		{
			public LargeWorkerContract()
				: base(typeof(MsgType1), typeof(MsgType2), typeof(MsgType3), typeof(MsgType4), typeof(MsgType5),
				typeof(MsgType6), typeof(MsgType7), typeof(MsgType8), typeof(MsgType9), typeof(MsgType10),
				typeof(MsgType11), typeof(MsgType12), typeof(MsgType13), typeof(MsgType14), typeof(MsgType15),
				typeof(MsgType16), typeof(MsgType17), typeof(MsgType18), typeof(MsgType19), typeof(MsgType20),
				typeof(MsgType21))
			{ }
		}

		class LargeWorkerImpl : LargeWorkerContract
		{
			public MsgType21 Data;

			[XcoConcurrent]
			void Process(MsgType21 data)
			{
				this.Data = data;
			}
		}

		[Test]
		public void TestRunWorkerWithVeryLargePortSet()
		{
			using (XcoAppSpace space1 = new XcoAppSpace("tcp.port=8000"),
				space2 = new XcoAppSpace("tcp.port=8001"))
			{
				var worker = new LargeWorkerImpl();
				space1.RunWorker<LargeWorkerContract>(worker);

				var remotePort = space2.ConnectWorker<LargeWorkerContract>(Environment.MachineName + ":8000");
				
				Assert.IsNull(worker.Data);
				remotePort.PostUnknownType(new MsgType21());

				Thread.Sleep(1000);
				Assert.IsNotNull(worker.Data);
			}
		}
	}
}
